#!/bin/sh

test_description="test ref review status"

. ./lib/sharness.sh

REPO_ID=""

#创建repo
test_expect_success "[test-1] init repo and branch" '
	REPO_ID=$(find_repo | jq .id)
	if [ -n $REPO_ID ]; then
		delete_repo > delete_repo.resp 2>&1
		test_expect_code 0
	fi
	create_repo > create_repo.resp 2>&1
	REPO_ID=$(cat create_repo.resp) &&
	add_member $WRITE_LEVEL $another_user_external &&
	init_repo > init_repo.resp &&
	test_expect_code 0
'

#创建 master 分支
test_expect_success "[test-2] normal push success" '
	echo "This is the first commit" > "$REPO_DIR"/README &&
	git -C "$REPO_DIR" add README &&
	git -C "$REPO_DIR" commit -m "first commit" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push origin master > test-2 2>&1 &&
	test_expect_code 0
'

test_expect_success "[test-3] create branch of a ref review will conflict" '
	add_member $WRITE_LEVEL $another_user_external &&
	enable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" checkout -b dev &&
	git -C "$REPO_DIR" push origin HEAD:dev  > test-3-1 2>&1 &&
	grep "has been created" test-3-1 &&
	disable_ref_review $REPO_ID &&
        git -C "$REPO_DIR" push -u origin dev > test-3-2 2>&1 &&
        grep "new branch" test-3-2 &&
        query_ref_review $REPO_ID 1 > query &&
        pass=$(cat query | jq .todo_list.requirement_rule_items[0].pass) &&
        echo $pass | grep false
'

test_expect_success "[test-4] update branch of a ref review will conflict" '
	git -C "$REPO_DIR" checkout master &&
	echo "[test-4] update ref of a ref review will conflict line 1" >> "$REPO_DIR"/test.file &&
	git -C "$REPO_DIR" add test.file &&
	git -C "$REPO_DIR" commit -m "commit1" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	echo "[test-4] update ref of a ref review will conflict line 2" >> "$REPO_DIR"/test.file &&
	git -C "$REPO_DIR" add test.file &&
        git -C "$REPO_DIR" commit -m "commit2" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	disable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push origin master &&
	git -C "$REPO_DIR" reset --hard HEAD~2 &&
	enable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push --force origin master > test-4 2>&1 &&
	grep "has been created" test-4 &&
	disable_ref_review $REPO_ID &&
	echo "[test-4] update ref of a ref review will conflict line 3" >> "$REPO_DIR"/test.file &&
	git -C "$REPO_DIR" add test.file &&
	git -C "$REPO_DIR" commit -m "commit3" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push --force origin master &&
	query_ref_review $REPO_ID 2 > query &&
        pass=$(cat query | jq .todo_list.requirement_rule_items[0].pass) &&
        echo $pass | grep false
'

test_expect_success "[test-5] delete branch of a ref review will conflict" '
	enable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push origin :dev > test-5-1 2>&1 &&
	grep "has been created" test-5-1 &&
	disable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push origin :dev &&
	query_ref_review $REPO_ID 3 > query &&
        pass=$(cat query | jq .todo_list.requirement_rule_items[0].pass) &&
        echo $pass | grep false
'

test_expect_success "[test-6] create tag of a ref review will conflict" '
	enable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" checkout master &&
	git -C "$REPO_DIR" tag v1.0 &&
	git -C "$REPO_DIR" push origin --tags  > test-6-1 2>&1 &&
	grep "has been created" test-6-1 &&
	disable_ref_review $REPO_ID &&
        git -C "$REPO_DIR" push origin --tags > test-6-2 2>&1 &&
        grep "new tag" test-6-2 &&
        query_ref_review $REPO_ID 4 > query &&
        pass=$(cat query | jq .todo_list.requirement_rule_items[0].pass) &&
        echo $pass | grep false
'

test_expect_success "[test-7] update tag of a ref review will conflict" '
	git -C "$REPO_DIR" checkout master &&
	echo "[test-7] update tag of a ref review will conflict" >> "$REPO_DIR"/test.file &&
        git -C "$REPO_DIR" add test.file &&
        git -C "$REPO_DIR" commit -m "commit1" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	enable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push origin HEAD:refs/tags/v1.0 --force > test-7-1 2>&1 &&
	grep "has been created" test-7-1 &&
	disable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push origin HEAD:refs/tags/v1.0 --force > test-7-2 2>&1 &&
	query_ref_review $REPO_ID 5 > query &&
        pass=$(cat query | jq .todo_list.requirement_rule_items[0].pass) &&
        echo $pass | grep false
'

test_expect_success "[test-8] delete tag of a ref review will conflict" '
	enable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push origin :refs/tags/v1.0 > test-8 2>&1 &&
	grep "has been created" test-8 &&
	disable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push origin :refs/tags/v1.0 > test-8-2 2>&1 &&
	grep "deleted" test-8-2 &&
	query_ref_review $REPO_ID 6 > query &&
        pass=$(cat query | jq .todo_list.requirement_rule_items[0].pass) &&
        echo $pass | grep false
'

test_expect_success "[test-9] clean up" '
  delete_repo "$REPO_ID" > test-10 2>&1
'

test_done